package com.tensquare.search.service;

import com.tensquare.search.dao.ArticleDao;
import com.tensquare.search.pojo.Article;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import util.IdWorker;

/**
 * Created by WF on 2020/10/22 11:23
 */
@Service
public class ArticleService {

    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private IdWorker idWorker;

    //1.添加到索引库中
    public void add(Article article) {
        //1.1)设置主键
        article.setId(idWorker.nextId() + "");
        //1.2)保存到索引库中
        articleDao.save(article);
    }
    //2.根据标题或内容进行文章的搜索
    public PageResult<Article> search(String keywords, int page, int size) {
        Page<Article> articlePage = articleDao.findByTitleOrContentLike(keywords, keywords, PageRequest.of(page - 1, size));
        return new PageResult<>(articlePage.getTotalElements(),articlePage.getContent());
    }
}
